package org.nokatag.tagjava;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.nokatag.system.Concest;

/**
 * 加载js和css
 * @author rebin
 *
 */
public class UseJavaScript extends BodyNokaTag{
	private static final long serialVersionUID = -4615369628998272150L;
	private String charset=Concest.ENCODING;
	private String rel = null;
	
	public int doStartTag()throws JspException{
		return EVAL_BODY_BUFFERED;
	}
	public int doAfterBody() throws JspException {
        BodyContent body = this.getBodyContent();
        String    content = body.getString();
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        try{
        	List<File> fils = new ArrayList<File>();
        	//--------------初始化加载列表------------------------------------------------------------------------------------------
        	String[] inps =content.replace("\r", "").replace("\t", "").replaceAll("\n", "").split(";");
        	for(int i=0;i<inps.length;i++){
        		String dirfile = inps[i];
        		if(dirfile.endsWith(".*")){//目录
        			dirfile = dirfile.substring(0,dirfile.length()-".*".length());;
        		}
        		dirfile = dirfile.replaceAll("\\.","/");
        		File dir = new File(Concest.ROOTDIR+dirfile);
        		if(dir.exists()){
    				File[] file =dir.listFiles();
    				fils.addAll(Arrays.asList(file));
    			}else{
    				File dirjs = new File(dir.getPath()+".js");
        			File dircs = new File(dir.getPath()+".css");
        			if(dirjs.exists()){
        				fils.add(dirjs);
        			}
        			if(dircs.exists()){
        				fils.add(dircs);
        			}
    			}
        	}
        	//-------------输出加载代码---------------------------------------------------------------------------------------------
        	StringBuffer sb = new StringBuffer();
        	for(File file:fils){
        		String name = file.getName();
        		String Prefixes=name.substring(name.lastIndexOf("."),name.length());//文件扩展名
        		String path = file.getPath().replace("\\", "/");
        		path = path.substring(Concest.ROOTDIR.length(),path.length());
        		if(file.exists()){
        			if(null==request.getAttribute(path)){
		        		if(".css".equalsIgnoreCase(Prefixes)){
		        			sb.append("<link type=\"text/css\" href=\""+basepath(request)+"/"+path+"?v="+Concest.SYSTEM_START_TIME.getTime()+"\" "+(null==rel?"":"rel=\""+rel+"\"")+"/>\n");
		        		}
		        		if(".js".equalsIgnoreCase(Prefixes)){
		        			sb.append("<script type=\"text/javascript\" src=\""+basepath(request)+"/"+path+"?v="+Concest.SYSTEM_START_TIME.getTime()+"\" "+(null==charset?"":"charset=\""+charset+"\"")+"></script>\n");
		        		}
		        		request.setAttribute(path,"true");
        			}
        		}
        	}
	        body.clearBody();
	        body.write(sb.toString());
	        setBodyContent(body);
        }catch(Exception se){
        	se.printStackTrace();
        }
        return SKIP_BODY;
    }
    public int doEndTag()throws JspException{  
        if(super.bodyContent!=null){  
            try{  
                super.bodyContent.writeOut(super.getPreviousOut());    //输出到页面  
            }  
            catch(Exception e){}  
        }  
        return BodyTagSupport.EVAL_PAGE;  
    }
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	public String getRel() {
		return rel;
	}
	public void setRel(String rel) {
		this.rel = rel;
	}
}
